package com.honeymoondesert.main;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.gson.JsonObject;
import com.honeymoondesert.crawler.CommentsInfo;
import com.honeymoondesert.crawler.StoreInfo;
import com.honeymoondesert.util.FileHelper;
import com.honeymoondesert.util.MapUtil;
import com.honeymoondesert.util.PropertyHelper;
public class SingleExport {
	Logger log=Logger.getLogger(this.getClass());
	
	String storeUrl;
	String baseStorePath;
	String dishesPath;
	String surroundPath;
	String impressionPath;
	String commentPath;

	public SingleExport(String storeUrl){
		PropertyHelper ph=new PropertyHelper(null);
		String output=ph.getProperties("output");
		this.storeUrl=storeUrl;
		this.baseStorePath=output+"baseStore\\";
		this.dishesPath=output+"dishes\\";
		this.surroundPath=output+"surround\\";
		this.impressionPath=output+"impression\\";
		this.commentPath=output+"comment\\";
	}
	public void exportAllInfo(String beforeDate) {
		log.info("开始爬取:"+storeUrl);
		StoreInfo si=new StoreInfo(storeUrl);
		//本店信息
		JsonObject baseStore=si.crawlStoreInfo();
		File baseDir=new File(baseStorePath);
		baseDir.mkdirs();
		FileHelper.writeFile(baseStorePath+baseStore.get("id").getAsString().trim(), baseStore.toString());
		//推荐菜
		List<JsonObject> dishes=si.getDishes();
		dishesPath+=si.storeId+"\\";
		File dishesDir=new File(dishesPath);
		dishesDir.mkdirs();
		for (JsonObject dish : dishes) {
			FileHelper.writeFile(dishesPath+dish.get("name").getAsString().trim(), dish.toString());
		}
		//大家印象
		List<JsonObject> impression=si.getImpression();
		impressionPath+=si.storeId+"\\";
		File impDir=new File(impressionPath);
		impDir.mkdirs();
		for (JsonObject imp : impression) {
			FileHelper.writeFile(impressionPath+imp.get("name").getAsString().trim(), imp.toString());
		}
		//爬取点评
		CommentsInfo ci=new CommentsInfo();
		ci.setCommentUrlFromStoreUrl(si.storeUrl);
		List<JsonObject> comments=ci.getCommentsBeforeDate(beforeDate);
		if(comments!=null){
			commentPath+=si.storeId+"\\";
			File cmtDir=new File(commentPath);
			cmtDir.mkdirs();
			for (JsonObject comment : comments) {
				FileHelper.writeFile(commentPath+
						comment.get("postDate").getAsString().trim()+
						comment.get("username").getAsString().trim(), comment.toString());
			}
		}else {
			log.error("抓取点评失败");
		}
		
		//周边店面信息
		List<String> urls=si.getSurroundUrls();
		if(urls!=null){
			surroundPath+=si.storeId+"\\";
			File surroundDir=new File(surroundPath);
			surroundDir.mkdirs();
			String district = baseStore.get("district").getAsString();
			String address = district + baseStore.get("address").getAsString();
			String city = baseStore.get("city").getAsString();
			MapUtil mapUtil = new MapUtil();
			for (String url : urls) {
				StoreInfo surround=new StoreInfo(url);
				JsonObject surroundStore=surround.crawlStoreInfo();
				if(!surroundStore.equals(new JsonObject())){
					String address2 = surroundStore.get("address")==null?"":surroundStore.get("address").getAsString();
					String city2 = surroundStore.get("city")==null?"":surroundStore.get("city").getAsString();
					Double d = null;
					try {
						  d = mapUtil.getDistance(address, address2, city, city2);
					} catch (IOException e) {
						log.error("得到距离有误！");
						e.printStackTrace();
					}
					surroundStore.addProperty("distance", d==null?"":d.toString());
					FileHelper.writeFile(surroundPath+surroundStore.get("id").getAsString().trim(), surroundStore.toString());
				}
				
			}
		}else {
			log.error("抓取附近店面信息失败");
		}
		
		log.info("爬虫结束:"+storeUrl);
	}
	public static void main(String[] args) {
//		SingleExport se=new SingleExport("http://www.dianping.com/shop/21104430");
		SingleExport se=new SingleExport("http://www.dianping.com/shop/5446138");
		se.exportAllInfo("2014-01-01");
	}
}
